{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4076637d",
   "metadata": {},
   "source": [
    "Here we have 3 bots - Gpt, Gemini and Claude. They are trying to find the top 3 stocks on NYSE which all the 3 bots identify, review and finalize when in consensus. The call to the model is kept with a range of 100 in for loop to mimic infinite loop as a breaking condition is set for when the 3 bots are in consensus for the top 3 stocks.\n",
    "I would like to invite the reader to go through the code and share any feedbacks that could help me improve more on this. Any suggestions and feedbacks are most welcome. You could send your feedback at - srbmisc@gmail.com.\n",
    "\n",
    "Thank You"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8c8a1f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from openai import OpenAI\n",
    "import anthropic\n",
    "from IPython.display import Markdown, display, update_display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24b06e47",
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv(override=True)\n",
    "openai_api_key = os.getenv('OPENAI_API_KEY')\n",
    "anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
    "google_api_key = os.getenv('GOOGLE_API_KEY')\n",
    "\n",
    "if openai_api_key:\n",
    "    print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
    "else:\n",
    "    print(\"OpenAI API Key not set\")\n",
    "    \n",
    "if anthropic_api_key:\n",
    "    print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
    "else:\n",
    "    print(\"Anthropic API Key not set\")\n",
    "\n",
    "if google_api_key:\n",
    "    print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n",
    "else:\n",
    "    print(\"Google API Key not set\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d687412b",
   "metadata": {},
   "outputs": [],
   "source": [
    "openai = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c5d430e",
   "metadata": {},
   "outputs": [],
   "source": [
    "sp_gpt = '''You are a bot Gpt. You are in a conversation with 2 other bots Gemini and Claude. All 3 of you are trying to figure out the top 3 stocks that have performed so far in NYSE. \n",
    "At every turn, you propose a stock, share its performance and fundamentals, and ask for the others for their review on this stock. Similarly, when its their turn and they share their stock pick, you review their pick.\n",
    "If you think thier pick was better, accept it and in your next turn share that same stock otherwise ask them to accept your pick. The goal is to come up with 3 stocks at the end that all 3 participants consider the best.\n",
    "If there is a concensus on 3 top stocks and its your turn, just output like this CONSENSUS REACHED : stock 1, stock 2, stock 3\n",
    "Prefix your response with Gpt: in bold and respond in Markdown'''\n",
    "\n",
    "sp_gemini = '''You are a bot Gemini. You are in a conversation with 2 other bots Gpt and Claude. All 3 of you are trying to figure out the top 3 stocks that have performed so far in NYSE. \n",
    "At every turn, you propose a stock, share its performance and fundamentals, and ask for the others for their review on this stock. Similarly, when its their turn and they share their stock pick, you review their pick.\n",
    "If you think thier pick was better, accept it and in your next turn share that same stock otherwise ask them to accept your pick. The goal is to come up with 3 stocks at the end that all 3 participants consider the best.\n",
    "If there is a concensus on 3 top stocks and its your turn, just output like this CONSENSUS REACHED : stock 1, stock 2, stock 3\n",
    "Prefix your response with Gemini: in bold and respond in Markdown'''\n",
    "\n",
    "sp_claude = '''You are a bot Claude. You are in a conversation with 2 other bots Gemini and Gpt. All 3 of you are trying to figure out the top 3 stocks that have performed so far in NYSE. \n",
    "At every turn, you propose a stock, share its performance and fundamentals, and ask for the others for their review on this stock. Similarly, when its their turn and they share their stock pick, you review their pick.\n",
    "If you think thier pick was better, accept it and in your next turn share that same stock otherwise ask them to accept your pick. The goal is to come up with 3 stocks at the end that all 3 participants consider the best.\n",
    "If there is a concensus on 3 top stocks and its your turn, just output like this CONSENSUS REACHED : stock 1, stock 2, stock 3\n",
    "Prefix your response with Claude: in bold and respond in Markdown'''\n",
    "\n",
    "talk = \"Gpt: Hello Gemini, Hello Claude. I want to discuss with you a good stock on the NYSE with you.<br> Gemini: Hello Gpt, Hello Claude. Sure go ahead, give us the best stock you think is there on the NYSE ?<br> Claude: Hello Gpt, Hello Gemini. Sure Gpt, lets discuss on some stocks. What stock do you have on mind ?<br>\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8eb58eae",
   "metadata": {},
   "outputs": [],
   "source": [
    "def callBot(mode):\n",
    "\n",
    "    global talk\n",
    "    talk = talk + \"<br><br><br>\"\n",
    "    messages = [{\"role\": \"system\", \"content\": sp_gpt if mode==0 else (sp_gemini if mode==1 else sp_claude)},\n",
    "    {\"role\":\"user\", \"content\":talk}]\n",
    "\n",
    "    if mode==0:\n",
    "        model = 'gpt-4.1-mini'\n",
    "        client = OpenAI()\n",
    "    elif mode==1:\n",
    "        model = 'gemini-2.5-flash'\n",
    "        client = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
    "    else:\n",
    "        model = 'claude-3-5-haiku-latest'\n",
    "        client = OpenAI(api_key=anthropic_api_key, base_url=\"https://api.anthropic.com/v1/\")\n",
    "\n",
    "    stream = client.chat.completions.create(\n",
    "        model=model,\n",
    "        messages=messages,\n",
    "        stream=True\n",
    "    )\n",
    "    for chunk in stream:\n",
    "        talk += (chunk.choices[0].delta.content or '')\n",
    "        talk = talk.replace(\"```\",\"\").replace(\"markdown\",\"\")\n",
    "        update_display(Markdown(talk), display_id=display_handle.display_id)\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a3e9ebc",
   "metadata": {},
   "outputs": [],
   "source": [
    "display_handle = display(Markdown(\"\"), display_id=True) \n",
    "\n",
    "for i in range(100):\n",
    "    callBot(i%3)\n",
    "    if 'CONSENSUS REACHED :' in talk or 'CONSENSUS REACHED:' in talk:\n",
    "        break\n",
    "\n",
    "   "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
